package com.dykj.fireFighting.web.base.websocket.configure;

import com.dykj.fireFighting.web.base.websocket.listener.MoreMessageListener;
import com.dykj.fireFighting.web.base.websocket.listener.RedisListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * @Author fgc
 * @Date 2021/7/30 16:06
 * @ Description：消息订阅配置类
 */
@Configuration
public class RedisSubscriberConfig {
    /**
     * 消息监听适配器，注入接受消息方法
     *
     * @param receiver
     * @return
     */
    @Bean
    public MessageListenerAdapter messageListenerAdapter(MoreMessageListener receiver) {
        return new MessageListenerAdapter(receiver);
    }

    @Bean
    public MessageListenerAdapter fireMessageListenerAdapter(RedisListener receiver) {
        return new MessageListenerAdapter(receiver);
    }

    /**
     * 创建消息监听容器
     *
     * @param redisConnectionFactory
     * @param messageListenerAdapter
     * @return
     */
    @Bean
    public RedisMessageListenerContainer getRedisMessageListenerContainer(RedisConnectionFactory redisConnectionFactory, MessageListenerAdapter messageListenerAdapter, MessageListenerAdapter fireMessageListenerAdapter) {
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
        redisMessageListenerContainer.addMessageListener(messageListenerAdapter, new PatternTopic("channel:moreMessage"));
        //监听指定db0,db1的set事件
        redisMessageListenerContainer.addMessageListener(fireMessageListenerAdapter, new PatternTopic("channel:fireEngineMessage"));
        return redisMessageListenerContainer;
    }
}
